package com.zn.listener;

import com.alibaba.fastjson.JSONObject;
import com.zn.util.GZIPUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 订阅到redis消息
 */
@Component
public class RedisReceiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReceiver.class);
    public void tradeReportNotify(String mapToJson) {
        try {
            JSONObject json = JSONObject.parseObject(mapToJson);
            String data=json.get("data").toString();
            String uuid=json.get("uuid").toString();
            LOGGER.warn("接受到交易过来的回报数据：" + mapToJson);
            //将回报数据推送给客户端 以二进制推送 推送压缩后的包
            byte [] byteData= GZIPUtils.compress(data.toString());
            ByteBuf byteBuf = Unpooled.buffer(byteData.length * 3);
            byteBuf.writeBytes(byteData);
            String jieya=GZIPUtils.uncompressToString(byteData);
            LOGGER.warn("tradeReportNotify 解压后的数据包" + jieya);
            //文本形式推送
//          ChannelGroups.writeByUuid(uuid, new TextWebSocketFrame(data));
        }catch (Exception e){
            LOGGER.error("tradeReportNotify Exception.......",e);
        }


    }

}